package com.lej.theme.dialog

import android.content.Context
import android.graphics.Color
import android.widget.Button
import androidx.core.view.isGone
import com.lej.theme.R
import com.lej.theme.databinding.ThemeDialogConfirmBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.qmuiteam.qmui.kotlin.onClick

class CustomAlertPopDialog(
    context: Context, var title: String?, var content: String?,
    var confirmText: String = "确认",
    var cancelText: String = "取消",
    val hideCancel: Boolean = false,
    val block: ThemeDialogConfirmBinding.() -> Unit = {},
    val cancel: () -> Unit = {},
    val onConfirm: () -> Unit = {}
) : CenterPopupView(context) {
    override fun getImplLayoutId() = R.layout.theme_dialog_confirm
    override fun initPopupContent() {
        super.initPopupContent()
        val binding = ThemeDialogConfirmBinding.bind(centerPopupContainer.findViewById(R.id.cl_dialog))
        binding.tvTitle.text = title
        binding.tvSubTitle.text = content
        binding.tvTitle.isGone = title.isNullOrEmpty()
        binding.vDivider.isGone = title.isNullOrEmpty()
        binding.tvSubTitle.isGone = content.isNullOrEmpty()

        val btnPositive: Button
        val btnNegative: Button
        if (positiveIndex == 1) {
            btnPositive = binding.btnPositive
            btnNegative = binding.btnNegative
        } else {
            btnPositive = binding.btnNegative
            btnNegative = binding.btnPositive
        }
        btnPositive.setBackgroundColor(Color.parseColor("#FF6BB5FA"))
        btnPositive.setTextColor(Color.WHITE)
        btnPositive.text = confirmText
        btnNegative.text = cancelText

        btnNegative.isGone = hideCancel

        btnNegative.onClick {
            dismiss()
        }
        btnPositive.onClick {
            isConfirmDismiss = true
            onConfirm()
            dismiss()
        }
        binding.block()
    }

    ///是否确认关闭
    var isConfirmDismiss = false
    override fun focusAndProcessBackPress() {
        super.focusAndProcessBackPress()
    }

    override fun dismiss() {
        super.dismiss()
        if (!isConfirmDismiss) cancel()
    }

    ///积极的按钮
    private var positiveIndex = 1
    fun setPositiveIndex(positiveIndex: Int): CustomAlertPopDialog {
        this.positiveIndex = positiveIndex
        return this
    }

    companion object {
        fun buildXPopup(
            context: Context,
            title: String? = "",
            content: String?,
            confirmText: String = "确认",
            cancelText: String = "取消",
            hideCancel: Boolean = false,
            block: ThemeDialogConfirmBinding.() -> Unit = {},
            cancel: () -> Unit = {},
            onConfirm: () -> Unit = {}
        ) = XPopup.Builder(context)
            .dismissOnTouchOutside(true)
            .asCustom(
                CustomAlertPopDialog(
                    context,
                    title = title,
                    content = content,
                    confirmText = confirmText,
                    cancelText = cancelText,
                    hideCancel = hideCancel,
                    block = block,
                    cancel = cancel,
                    onConfirm = onConfirm
                )
            )
    }
}